package com.steven.team.controller;

import com.alibaba.fastjson.JSONObject;
import com.steven.team.pojo.TeamApply;
import com.steven.team.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class TeamController {

    @Autowired
    TeamService teamService;

    @PostMapping("/team/add")
    public JSONObject addTeam(@RequestHeader Long uid, Long matchId, String teamName, String matchName, String url, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0 || teamName.isEmpty() || matchName.isEmpty()) {
            response.sendError(500, "传参错误");
            return null;
        }
        return teamService.addTeam(uid, matchId, teamName, matchName, url);
    }

    @PostMapping("/team/updateName")
    public JSONObject updateTeamName(@RequestHeader Long uid, Long teamId, String teamName, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0 || teamId == 0 || teamName.isEmpty()) {
            response.sendError(500, "传参错误");
            return null;
        }
        return teamService.updateTeamName(teamName, teamId);
    }

    @PostMapping("/team/updateStatus")
    public JSONObject updateTeamStatus(@RequestHeader Long uid, Long teamId, Boolean status, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0 || teamId == 0 || status == null) {
            response.sendError(500, "传参错误");
            return null;
        }
        return teamService.updateTeamStatus(teamId, status);
    }

    @PostMapping("/team/apply")
    public JSONObject applyToTeam(@RequestHeader Long uid, Long teamId, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0 || teamId == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        TeamApply apply = new TeamApply();
        apply.setTeamId(teamId);
        apply.setUid(uid);
        apply.setCreateTime(System.currentTimeMillis());
        return teamService.applyToTeam(apply);
    }

    @PostMapping("/team/queryUserTeam")
    public JSONObject queryUserTeam(@RequestHeader Long uid, Long user, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return teamService.queryUserTeam(uid, user);
    }

    @PostMapping("/team/updateApply")
    public JSONObject updateApplyToTeam(@RequestHeader Long uid, Long applyId, boolean pass, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return teamService.updateApplyToTeam(applyId, pass);
    }

    @PostMapping("/team/queryApply")
    public JSONObject queryApplyToMe(@RequestHeader Long uid, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return teamService.queryApplyToMe(uid);
    }

    @PostMapping("/team/queryDetail")
    public JSONObject queryTeamDetail(@RequestHeader Long uid, Long teamId, HttpServletResponse response) throws IOException {
        if (uid == null) {
            response.sendError(500, "传参错误");
            return null;
        }
        return teamService.queryTeamDetail(uid, teamId);
    }

    @PostMapping("/match/search")
    public JSONObject searchMatchList(@RequestHeader Long uid, String matchName, Long lastId, HttpServletResponse response) throws IOException {
        if (uid == null) {
            response.sendError(500, "传参错误");
            return null;
        }
        return teamService.searchMatch(matchName, lastId);
    }

    @PostMapping("/team/searchTop5")
    public JSONObject searchTop5Team(@RequestHeader Long uid, HttpServletResponse response) throws IOException {
        if (uid == null) {
            response.sendError(500, "传参错误");
            return null;
        }
        return teamService.queryTop5Team(uid);
    }

    @PostMapping("/team/addPageViews")
    public JSONObject addTeamPageViews(@RequestHeader Long uid, Long teamId, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0 || teamId == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return teamService.addTeamPageViews(teamId);
    }

    @PostMapping("/team/queryCollections")
    public JSONObject queryTeamCollections(@RequestHeader Long uid, Long lastCreateTime, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return teamService.queryTeamCollections(uid, lastCreateTime);
    }

    @PostMapping("/team/addCollection")
    public JSONObject addToTeamCollection(@RequestHeader Long uid, Long teamId, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0 || teamId == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return teamService.addToTeamCollection(teamId, uid);
    }

    @PostMapping("/team/deleteCollection")
    public JSONObject deleteTeamCollection(@RequestHeader Long uid, Long teamId, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0 || teamId == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return teamService.deleteTeamCollection(uid, teamId);
    }

    @PostMapping("/ban/add")
    public JSONObject addBan(@RequestHeader Long uid, Long userId, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0 || userId == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return teamService.addUserBan(userId);
    }

    @PostMapping("/ban/delete")
    public JSONObject deleteBan(@RequestHeader Long uid, Long userId, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0 || userId == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return teamService.deleteUserBane(userId);
    }

    @PostMapping("/team/querySameSchool")
    public JSONObject searchSameSchoolTeam(@RequestHeader Long uid, HttpServletResponse response) throws IOException {
        if (uid == null || uid == 0) {
            response.sendError(500, "传参错误");
            return null;
        }
        return teamService.searchSameSchoolTeam(uid);
    }

    @PostMapping("/team/queryNew")
    public JSONObject searchNewCreateTeam(@RequestHeader Long uid, Long lastCreateTime, HttpServletResponse response) throws IOException {
        return teamService.searchNewCreateTeam(uid,lastCreateTime);
    }
}
